package com.zero.logistics.mapper;

import com.zero.logistics.pojo.Order;
import com.zero.logistics.pojo.OrderExample.Criteria;
import com.zero.logistics.pojo.OrderExample.Criterion;
import com.zero.logistics.pojo.OrderExample;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

public class OrderSqlProvider {
    public String insertSelective(Order record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("t_order");
        
        if (record.getOrderId() != null) {
            sql.VALUES("order_id", "#{orderId,jdbcType=BIGINT}");
        }
        
        if (record.getUserId() != null) {
            sql.VALUES("user_id", "#{userId,jdbcType=BIGINT}");
        }
        
        if (record.getCustomerId() != null) {
            sql.VALUES("customer_id", "#{customerId,jdbcType=BIGINT}");
        }
        
        if (record.getShippingAddress() != null) {
            sql.VALUES("shipping_address", "#{shippingAddress,jdbcType=VARCHAR}");
        }
        
        if (record.getShippingName() != null) {
            sql.VALUES("shipping_name", "#{shippingName,jdbcType=VARCHAR}");
        }
        
        if (record.getShippingPhone() != null) {
            sql.VALUES("shipping_phone", "#{shippingPhone,jdbcType=VARCHAR}");
        }
        
        if (record.getTakeName() != null) {
            sql.VALUES("take_name", "#{takeName,jdbcType=VARCHAR}");
        }
        
        if (record.getTakeAddress() != null) {
            sql.VALUES("take_address", "#{takeAddress,jdbcType=VARCHAR}");
        }
        
        if (record.getTakePhone() != null) {
            sql.VALUES("take_phone", "#{takePhone,jdbcType=VARCHAR}");
        }
        
        if (record.getOrderStatus() != null) {
            sql.VALUES("order_status", "#{orderStatus,jdbcType=INTEGER}");
        }
        
        if (record.getPaymentMethodId() != null) {
            sql.VALUES("payment_method_id", "#{paymentMethodId,jdbcType=BIGINT}");
        }
        
        if (record.getRegionId() != null) {
            sql.VALUES("region_id", "#{regionId,jdbcType=BIGINT}");
        }
        
        if (record.getTakeMethodId() != null) {
            sql.VALUES("take_method_id", "#{takeMethodId,jdbcType=BIGINT}");
        }
        
        if (record.getFreightMethodId() != null) {
            sql.VALUES("freight_method_id", "#{freightMethodId,jdbcType=BIGINT}");
        }
        
        if (record.getOrderRemark() != null) {
            sql.VALUES("order_remark", "#{orderRemark,jdbcType=VARCHAR}");
        }
        
        return sql.toString();
    }

    public String selectByExample(OrderExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("order_id");
        } else {
            sql.SELECT("order_id");
        }
        sql.SELECT("user_id");
        sql.SELECT("customer_id");
        sql.SELECT("shipping_address");
        sql.SELECT("shipping_name");
        sql.SELECT("shipping_phone");
        sql.SELECT("take_name");
        sql.SELECT("take_address");
        sql.SELECT("take_phone");
        sql.SELECT("order_status");
        sql.SELECT("payment_method_id");
        sql.SELECT("region_id");
        sql.SELECT("take_method_id");
        sql.SELECT("freight_method_id");
        sql.SELECT("order_remark");
        sql.FROM("t_order");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }
    
    /**
     * @function 自定义批量删除订单
     * @param ids
     * @return
     */
    public String deleteByIds(@Param("ids")String[] ids) {
    	StringBuilder sb = new StringBuilder();
    	sb.append("delete from t_order where order_id in (");
    	for (int i = 0; i < ids.length; i++) {
			sb.append("#{ids[" + i + "]},");
		}
    	sb.deleteCharAt(sb.length() - 1); //删除最后一个逗号
    	sb.append(")");
    	System.out.println("批量删除的sql语句：" + sb.toString());
    	return sb.toString();
    }

    public String updateByPrimaryKeySelective(Order record) {
        SQL sql = new SQL();
        sql.UPDATE("t_order");
        
        if (record.getUserId() != null) {
            sql.SET("user_id = #{userId,jdbcType=BIGINT}");
        }
        
        if (record.getCustomerId() != null) {
            sql.SET("customer_id = #{customerId,jdbcType=BIGINT}");
        }
        
        if (record.getShippingAddress() != null) {
            sql.SET("shipping_address = #{shippingAddress,jdbcType=VARCHAR}");
        }
        
        if (record.getShippingName() != null) {
            sql.SET("shipping_name = #{shippingName,jdbcType=VARCHAR}");
        }
        
        if (record.getShippingPhone() != null) {
            sql.SET("shipping_phone = #{shippingPhone,jdbcType=VARCHAR}");
        }
        
        if (record.getTakeName() != null) {
            sql.SET("take_name = #{takeName,jdbcType=VARCHAR}");
        }
        
        if (record.getTakeAddress() != null) {
            sql.SET("take_address = #{takeAddress,jdbcType=VARCHAR}");
        }
        
        if (record.getTakePhone() != null) {
            sql.SET("take_phone = #{takePhone,jdbcType=VARCHAR}");
        }
        
        if (record.getOrderStatus() != null) {
            sql.SET("order_status = #{orderStatus,jdbcType=INTEGER}");
        }
        
        if (record.getPaymentMethodId() != null) {
            sql.SET("payment_method_id = #{paymentMethodId,jdbcType=BIGINT}");
        }
        
        if (record.getRegionId() != null) {
            sql.SET("region_id = #{regionId,jdbcType=BIGINT}");
        }
        
        if (record.getTakeMethodId() != null) {
            sql.SET("take_method_id = #{takeMethodId,jdbcType=BIGINT}");
        }
        
        if (record.getFreightMethodId() != null) {
            sql.SET("freight_method_id = #{freightMethodId,jdbcType=BIGINT}");
        }
        
        if (record.getOrderRemark() != null) {
            sql.SET("order_remark = #{orderRemark,jdbcType=VARCHAR}");
        }
        
        sql.WHERE("order_id = #{orderId,jdbcType=BIGINT}");
        
        return sql.toString();
    }

    protected void applyWhere(SQL sql, OrderExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            sql.WHERE(sb.toString());
        }
    }
}